﻿using System;
using System.Xml;
using System.Xml.Serialization;
using System.Drawing;
using System.Windows.Forms;
using rlTodayMakeSkin.Project.Attributes;
using rlTodayMakeSkin.Project.XmlElement.XmlSubElements;

namespace rlTodayMakeSkin.Project.XmlElement
{
    /// <summary>
    /// represents a digital clock skin element
    /// </summary>
    public class rlDigitalClock : ContainerSubElements
    {
        private string format;
        private string align;
        private Color colore;
        private int timeZone=0;

        private DateTime thisTime = DateTime.Now;

        /**********************
         CONSTRUCTORS
         ********************/
        public rlDigitalClock(XmlNode n, rlTodayProject p)
            : base(n, p)
        {
            initElement();
            if (n.Attributes["format"] != null) format = n.Attributes["format"].Value;
            if (n.Attributes["alignment"] != null) align = n.Attributes["alignment"].Value;
            if (n.Attributes["color"] != null) colore = GetColorFromRGB(n.Attributes["color"].Value.ToString());
            if (n.Attributes["timezone"] != null) timeZone = int.Parse(n.Attributes["timezone"].Value);
        }

        public rlDigitalClock(rlTodayProject p)
            : base(p)
        {
            initElement();
        }

        /// <summary>
        /// for the serialization
        /// </summary>
        public rlDigitalClock() { }

        /**********************
         PUBLIC MEMBERS
         ********************/
        /// <summary>
        /// Gets, sets time format value
        /// </summary>
        [PropertyDescription("Time format", PropertyTypes.TIME_FORMAT)]
        [XmlAttribute("format")]
        public string Format
        {
            get { return format; }
            set { format = value; }
        }

        /// <summary>
        /// Gets, sets text alignment value
        /// </summary>
        [PropertyDescription("Text alignment", PropertyTypes.ALIGNMENT)]
        [XmlAttribute("alignment")]
        public string Align
        {
            get { return align; }
            set { align = value; }
        }

        /// <summary>
        /// Gets, sets the number of the timezone used for this clock. Count starting 0, so timezone=“0” for the first timezone
        /// </summary>
        [PropertyDescription("The number of the timezone used for this clock. Count starting 0, so timezone=\"0\" for the first timezone", PropertyTypes.INTEGER)]
        [XmlAttribute("timezone")]
        public int TimeZone
        {
            get { return timeZone; }
            set { timeZone = value; }
        }

        /// <summary>
        /// Gets, sets digital clock text color
        /// </summary>
        [PropertyDescription("Gets, sets digital clock text color", PropertyTypes.COLOR)]
        [XmlIgnore()]
        public Color DigitalClockColor
        {
            get { return colore; }
            set { colore = value; }
        }

        /// <summary>
        /// Returns a string that represents RGB value of the digital clock text color
        /// </summary>
        [PropertyDescription("RGB value of digital clock text color", ReadOnlyProperty=true)]
        [XmlAttribute("color")]
        public string RGBColor
        {
            get { return GetRGBFromColor(colore); }
        }

        public override void DrawElement(Graphics g)
        {
            DrawString(g, GetFormattedDate(format, thisTime), font, colore);
        }

        /**********************
         PRIVATE MEMBERS
         ********************/
        private void initElement()
        {
            strNome = "DigitalClock";
            font = new rlFont(this);
            colore = Color.Black;
            format = "HH:mm";
        }
    }
}
